/**
 * Created by zlkmu on 2017/8/18.
 */
//辅助函数，处理嫌疑人费用审批窗口
function approveSuspectFee(){
    $('#app_suspect_btn').click(function(){
        $('#app_dialog').dialog({
            resizable: false,
            height: "auto",
            width: 430,
            modal: true,
            title: '嫌疑人费用报销审批',
            buttons: {
                '确定' :function(){
                    if($('#app_opinion_detail').val() == ""){
                        $.prompt("<h3>未填写审批意见，请填写</h3>", {
                            title: '错误提醒',
                            buttons: {'确定': true}
                        })
                    }
                    else{
                        needApprove--;
                        approveOpinion.approve_suspect = 1;
                        approveOpinion.suspect_opinion = {
                            'suspect_opinion' : $('#app_opinion').val(),
                            'suspect_opinion_detail' : $('#app_opinion_detail').val(),
                        };
                        changeState($('#app_suspect_btn'));
                        $(this).dialog("close");
                    }
                },
                '取消': function(){
                    $(this).dialog("close");
                }
            }
        });
    })
}

//辅助函数，处理未使用公务卡报销强制项目审批
function approveUnuseOfficialFee(){
    $('#app_unofficial_btn').click(function(){
        $('#app_dialog').dialog({
            resizable: false,
            height: "auto",
            width: 430,
            modal: true,
            title: '未使用公务卡报销强制费用审批',
            buttons: {
                '确定' :function(){
                    if($('#app_opinion_detail').val() == ""){
                        $.prompt("<h3>未填写审批意见，请填写</h3>", {
                            title: '错误提醒',
                            buttons: {'确定': true}
                        })
                    }
                    else{
                        needApprove--;
                        approveOpinion.approve_unofficial = 1;
                        approveOpinion.unofficial_opinion = {
                            'unofficial_opinion' : $('#app_opinion').val(),
                            'unofficial_opinion_detail' : $('#app_opinion_detail').val(),
                        };
                        changeState($('#app_unofficial_btn'));
                        $(this).dialog("close");
                    }

                },
                '取消': function(){
                    $(this).dialog("close");
                }
            }
        });
    })
}

//辅助函数，处理超标费用审批
function approveOverstandardFee(){
    $('#app_overstandard_btn').click(function(){
        $('#app_dialog').dialog({
            resizable: false,
            height: "auto",
            width: 430,
            modal: true,
            title: '嫌疑人费用报销审批',
            buttons: {
                '确定' :function(){
                    if($('#app_opinion_detail').val() == ""){
                        $.prompt("<h3>未填写审批意见，请填写</h3>", {
                            title: '错误提醒',
                            buttons: {'确定': true}
                        })
                    }
                    else{
                        needApprove--;
                        approveOpinion.approve_overstandard = 1;
                        approveOpinion.overstandard_opinion = {
                            'over_opinion' : $('#app_opinion').val(),
                            'over_opinion_detail' : $('#app_opinion_detail').val(),
                        };
                        changeState($('#app_overstandard_btn'));
                        $(this).dialog("close");
                    }

                },
                '取消': function(){
                    $(this).dialog("close");
                }
            }
        });
    })
}

//辅助函数，处理基本报销信息审批
function approveBasicFee(){
    $('#app_fee_btn').click(function(){
        $('#app_dialog').dialog({
            resizable: false,
            height: "auto",
            width: 430,
            modal: true,
            title: '嫌疑人费用报销审批',
            buttons: {
                '确定' :function(){
                    if($('#app_opinion_detail').val() == ""){
                        $.prompt("<h3>未填写审批意见，请填写</h3>", {
                            title: '错误提醒',
                            buttons: {'确定': true}
                        })
                    }
                    else{
                        needApprove--;
                        approveOpinion.basic_fee_opinion = {
                            'basic_fee_opinion' : $('#app_opinion').val(),
                            'basic_fee_opinion_detail' : $('#app_opinion_detail').val()
                        };
                        changeState($('#app_fee_btn'));
                        $(this).dialog("close");
                    }
                },
                '取消': function(){
                    $(this).dialog("close");
                }
            }
        });
    })
}

//辅助函数，处理状态的改变
function changeState(elem){
    $(elem).parent().next().removeClass('ui-state-error-text').html("<i class='ui-icon ui-icon-circle-check'></i>已审批")
}

//辅助函数，处理审批状态按下
function approveFeeInfo(){
    approveBasicFee();
    approveSuspectFee();
    approveOverstandardFee();
    approveUnuseOfficialFee();
    $('#approve_btn').click(function(){
        $('#app_type_dialog').dialog({
            resizable: false,
            height: "auto",
            width: 430,
            modal: true,
            buttons: {
                '确定' :function(){
                    if(needApprove > 0){
                        $.prompt("<h3>还有未审批的事项，请根据提示修改</h3>", {
                            title: '错误提醒',
                            buttons: {'确定': true}
                        })
                    }
                    else{
                        ApproveCurItem();
                        $(this).dialog("close");
                    }
                },
                '取消': function(){
                    $(this).dialog("close");
                }
            }
        });
    })
}

//辅助函数，部门审批出差信息
function ApproveCurItem(){
    $('#dataLoad').show();
    var userInfo = JSON.parse(window.localStorage.getItem('user_info'));
    var identity = window.localStorage.getItem('approve_type');
    approveOpinion.userID = userInfo.user_id;
    approveOpinion.approve_state = identity;
    approveOpinion.approve_id = curItem.fee_data.travel_fee_id;
    //console.log(JSON.stringify(approveOpinion));
    // //   console.log(JSON.stringify(approveOpinion));
    $.post(url+'BusinessFee/ApproveBusinessFee.php', approveOpinion,
        function(data){
            $('#dataLoad').hide();
            var t = JSON.parse(data);
            if(t.state == 1){
                $.prompt( "<h3>操作成功</h3>",{
                    title: '消息提醒',
                    buttons: {'确定': true},
                    submit: function(e, v, m, f){
                        if(v){
                            jumpPage('section_approve.html');
                        }
                    }
                });
            }
            else{
                $.prompt( "<h3>网络或数据库错误，请稍后再试</h3>",{
                    title: '消息提醒',
                    buttons: {'确定': true}
                });
            }
        })

}